#include "basic/utils.h"

#include "basic/platform.h"
//#include "../version_common.h"

#ifdef DK_PLATFORM_OS_WINDOWS
#include <windows.h>
#else
//#include <uuid/uuid.h>
#endif

namespace basic {
uint32_t gGetNextUniqueNumberID() {
  static uint32_t s_ID{0};
  return ++s_ID;
}

/*
NS_USE_STD

SLibraryLoadResult gLoadSharedLibrary(const char* URL, const char* funcName,
                                      void** func) {
#if defined(DK_PLATFORM_OS_WINDOWS)
  SLibraryLoadResult result;
  result.success = false;
  HMODULE h = LoadLibrary(URL);
  result.handle = h;
  if (h) {
    *func = GetProcAddress(h, funcName);
    if (*func == nullptr) {
      result.msg = "Can not get function : " + string(funcName);
    } else {
      result.msg = "successfully loaded";
      result.success = true;
    }
  } else {
    result.msg = "Can not load library";
  }
  return result;
#elif defined(DK_PLATFORM_OS_LINUX)
  // dlopen(xxx);
  return {};
#elif defined(DK_PLATFORM_OS_APPLE);
  return 0;
#else
#error "unknown platform!"
  return {};
#endif
}

bool gUnloadSharedLibrary(HMODULE handle) {
  return FreeLibrary(handle) == TRUE;
}

bool bFirst = true;
clock_t lasttime = 0;
clock_t elapsedTime = 0;  // ��ʱ��
long frameCount = 0;      // ��֡��

bool CalculateFPS(float& fps) {
  if (bFirst) {
    lasttime = clock();
    bFirst = false;
  }

  clock_t nowtime = clock();
  elapsedTime += (nowtime - lasttime);
  lasttime = nowtime;

  ++frameCount;

  if (elapsedTime >= 1000) {
    fps = (float)frameCount / (elapsedTime * 0.001f);

    frameCount = 0;
    elapsedTime = 0;
    return true;
  }
  return false;
}

void StringSplit(dkstr s, std::vector<dkstr>* out, char delimiter) {
  size_t pos = 0;
  dkstr token;
  while ((pos = s.find(delimiter)) != dkstr::npos) {
    token = s.substr(0, pos);
    out->emplace_back(token);
    s.erase(0, pos + 1);
  }
  out->emplace_back(s);
}

int CreateUUID() {
  static int ID = 0;
  return ID++;
#if defined(DK_PLATFORM_OS_WINDOWS)
  GUID guid;
  CoCreateGuid(&guid);
  // return guid;
#elif defined(DK_PLATFORM_OS_LINUX);
  uuid_generate(reinterpret_cast<unsigned char*>(&guid));
  return 0;
#elif defined(DK_PLATFORM_OS_APPLE);
  return 0;
#else
#error "unknown platform!"
  return 0;
#endif
}

dkstr GuidToString(const GUID& guid) {
  char buf[64] = {0};
#if defined(DK_PLATFORM_CC_GCC)
  snprintf(
#elif defined(DK_PLATFORM_CC_VC)
  _snprintf_s(
#else
#error "unknown compiler!"
#endif
      buf, sizeof(buf), "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
      guid.Data1, guid.Data2, guid.Data3, guid.Data4[0], guid.Data4[1],
      guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6],
      guid.Data4[7]);
  return dkstr(buf);
}
*/
} // namespace basic
